package com.example.security.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 自定义登录页面
 */
//@Configuration
public class WebSecurityConfig2 extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //开启httpBasic
        http.httpBasic().and();
        //关闭跨域
        http.csrf().disable();
        //设置自定义登录页面
        http.formLogin()
                .loginPage("/login.html")// 自定义登录页面路径
                // 自定义页面的登录路径，注意要与登录页面的action值一致，<form action="/authentication/form" method="post">
                .loginProcessingUrl("/authentication/form")
                //默认security的登录页面的action为/login
//                .loginProcessingUrl("/login")
                .and()
                .authorizeRequests() // 定义哪些URL需要被保护、哪些不需要被保护
                //设置放路径，login.html页面如果不放行则报错将您重定向的次数过多
                .antMatchers("/login.html").permitAll()
                // 其余请求都需要身份验证
                .anyRequest().authenticated();


    }

    public static void main(String[] args) {
        String encode = new BCryptPasswordEncoder().encode("123456");
        System.out.println(encode);
    }
}
